package com.zork.cmdb.webapi.api;

import com.github.pagehelper.PageInfo;
import com.zork.cmdb.webapi.common.exception.CmdbException;
import com.zork.cmdb.webapi.common.response.ResponseEntityEnum;
import com.zork.cmdb.webapi.service.ModuleInstanceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author chenjh
 * @date 2018/5/15
 * @description
 */
@RestController
@RequestMapping("/moduleInstances")
public class ModuleInstanceController {

    @Autowired
    private ModuleInstanceService moduleInstanceService;

    private static final Integer MAX_PAGE_SIZE = 1000;

    @ApiOperation(value = "获取实例列表", notes = "获取实例列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "moduleID", value = "模型ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", required = false, defaultValue = "10", dataType = "Integer")
    })
    @GetMapping("/{moduleID}")
    public ResponseEntityEnum listInstance(@PathVariable("moduleID") Integer moduleID,
                                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) throws CmdbException {
        if (pageSize > MAX_PAGE_SIZE) {
            return ResponseEntityEnum.PARAM_EROOR.setMsg("pageSize不能大于" + MAX_PAGE_SIZE);
        }
        PageInfo<Map<String, Object>> instanceList = moduleInstanceService.listInstance(moduleID, null, pageNum, pageSize);
        ResponseEntityEnum result = ResponseEntityEnum.SUCCESS.setData(instanceList);
        return result;
    }

    @ApiOperation(value = "获取单个实例信息", notes = "获取单个实例信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "moduleID", value = "模型ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "moduleInstanceID", value = "模型实例ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", required = false, defaultValue = "10", dataType = "Integer")
    })
    @GetMapping("/{moduleID}/{moduleInstanceID}")
    public ResponseEntityEnum getInstance(@PathVariable("moduleID") Integer moduleID,
                                          @PathVariable("moduleInstanceID") Integer instanceID,
                                          @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) throws CmdbException {
        if (pageSize > MAX_PAGE_SIZE) {
            return ResponseEntityEnum.PARAM_EROOR.setMsg("pageSize不能大于" + MAX_PAGE_SIZE);
        }
        PageInfo<Map<String, Object>> instanceList = moduleInstanceService.listInstance(moduleID, instanceID, pageNum, pageSize);
        ResponseEntityEnum result = ResponseEntityEnum.SUCCESS.setData(instanceList);
        return result;
    }
}
